package main

import (
	"database/sql"
	"fmt"
	"log"
	"time"

	_ "github.com/mattn/go-oci8"
)

func main() {
	sqlconn := "datakit@oraclet#obcluster/123456@10.200.14.240:2883/datakit"
	db, err := sql.Open("oci8", sqlconn)
	if err != nil {
		log.Fatalln(err)
	}
	defer db.Close()

	tick := time.NewTicker(2 * time.Second)
	defer tick.Stop()

	for {
		rows, err := db.Query(SLOW_QUERY)
		if err != nil {
			fmt.Printf("query failed: %v\n", err)
			return
		}

		defer rows.Close()

		for rows.Next() {
			fmt.Println("next...")
		}

		err = rows.Err()
		if err != nil {
			fmt.Printf("rows Err: %v\n", err) // rows Err: ORA-01455: converting column overflows integer datatype
		}

		<-tick.C
	}
}

// SLOW_QUERY selects table GV$SQL_AUDIT.
//
//nolint:stylecheck
// const SLOW_QUERY = `SELECT
//   TO_NUMBER(PLAN_HASH) AS PLAN_HASH
// FROM GV$SQL_AUDIT
// WHERE REQUEST_TIME > 1698029021724572 AND QUERY_SQL IS NOT NULL`

const SLOW_QUERY = `SELECT 
"TENANT_ID",
"SVR_IP",
"SVR_PORT",
"PLAN_ID",
"SQL_ID",
"TYPE",
"DB_ID",
"IS_BIND_SENSITIVE",
"IS_BIND_AWARE",
"STATEMENT",
"QUERY_SQL",
"SPECIAL_PARAMS",
"PARAM_INFOS",
"SYS_VARS",
TO_NUMBER(PLAN_HASH) AS PLAN_HASH,
"FIRST_LOAD_TIME",
"SCHEMA_VERSION",
"MERGED_VERSION",
"LAST_ACTIVE_TIME",
"AVG_EXE_USEC",
"SLOWEST_EXE_TIME",
"SLOWEST_EXE_USEC",
"SLOW_COUNT",
"HIT_COUNT",
"PLAN_SIZE",
"EXECUTIONS",
"DISK_READS",
"DIRECT_WRITES",
"BUFFERS_GETS",
"APPLICATION_WATI_TIME",
"CONCURRENCY_WAIT_TIME",
"USER_IO_WAIT_TIME",
"ROWS_PROCESSED",
"ELAPSED_TIME",
"CPU_TIME",
"LARGE_QUERYS",
"DELAYED_LARGE_QUERYS",
"DELAYED_PX_QUERYS",
"OUTLINE_VERSION",
"OUTLINE_ID",
"OUTLINE_DATA",
"HINTS_INFO",
"HINTS_ALL_WORKED",
"ACS_SEL_INFO",
"TABLE_SCAN",
"EVOLUTION",
"EVO_EXECUTIONS",
"EVO_CPU_TIME",
"TIMEOUT_COUNT",
"PS_STMT_ID",
TO_NUMBER(SESSID) AS SESSID,
"TEMP_TABLES",
"IS_USE_JIT",
"OBJECT_TYPE",
"PL_SCHEMA_ID",
"IS_BATCHED_MULTI_STMT"
FROM GV$PLAN_CACHE_PLAN_STAT`
